package com.example.t1;

import java.io.File;
import java.io.FileNotFoundException;

import android.R.string;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.t1.util.TakePicMan;
import com.example.t1.view.Crop_Canvas;

public class CutActivity extends Activity implements OnClickListener {
    private Crop_Canvas canvas = null;
    private int width, heigh, style;// 选择框的初始宽高

    private final int PIC_FROM_ALUBM = 0, PIC_FROM_CAMERA = 1, PIC_CUT = 2;
    /**
     * 给按钮随便定义的id
     */
    private final int id1 = 123, id2 = 321;
    // 总布局
    private RelativeLayout layout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// 不用xml文件方便移植
	layout = new RelativeLayout(this);
	layout.setBackgroundColor(0xff123456);
	addContentView(layout, new LayoutParams(-1, -1));

	this.init();
    }

    private void init() {
	Intent intent = getIntent();
	width = intent.getIntExtra("width", 200);
	heigh = intent.getIntExtra("heigh", 200);
	style = intent.getIntExtra("style", 0);
	addButton1();
	addButton2();
	// addCutView();
	skip();

    }

    /**
     * 
     * 描述：添加界面左侧 取消按钮
     * 
     * @throws
     * @date：2013-12-18 上午10:05:42
     * @author 智迅 zhixun123@gmail.com
     */
    private void addButton1() {
	Button button1 = new Button(this);
	button1.setText("取消");
	button1.setId(id1);
	button1.setOnClickListener(this);

	android.widget.RelativeLayout.LayoutParams params;
	params = new android.widget.RelativeLayout.LayoutParams(-2, -2);
	params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, button1.getId());
	layout.addView(button1, params);
    }

    /**
     * 
     * 描述：添加右侧取消按钮
     * 
     * @throws
     * @date：2013-12-18 上午10:05:57
     * @author 智迅 zhixun123@gmail.com
     */
    private void addButton2() {
	Button button2 = new Button(this);
	button2.setText("确定");
	button2.setId(id2);
	button2.setOnClickListener(this);
	android.widget.RelativeLayout.LayoutParams params;

	params = new android.widget.RelativeLayout.LayoutParams(-2, -2);
	params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, button2.getId());
	layout.addView(button2, params);
    }

    /**
     * 
     * 描述 ： 添加剪裁控件
     * 
     * @throws
     * @date：2013-12-18 上午10:07:52
     * @author 智迅 zhixun123@gmail.com
     */
    private void addCutView() {
	canvas = new Crop_Canvas(this);
	canvas.setId(456);
	android.widget.RelativeLayout.LayoutParams params;
	params = new android.widget.RelativeLayout.LayoutParams(-1, -1);
	params.addRule(RelativeLayout.ALIGN_BOTTOM, canvas.getId());
	layout.addView(canvas, params);
    }

    /**
     * 
     * 描述：跳转方法 进入此activity后 ，
     * 
     * @throws
     * @date：2013-12-18 上午10:09:05
     * @author 智迅 zhixun123@gmail.com
     */
    private void skip() {
	int code = getIntent().getIntExtra("style", 0);
	switch (code) {
	case PIC_FROM_ALUBM:
	    TakePicMan.requestPicFromAlbum(PIC_FROM_ALUBM);
	    Intent openAlbumIntent = new Intent(
		    Intent.ACTION_PICK,
		    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
	    startActivityForResult(openAlbumIntent, PIC_FROM_ALUBM);
	    break;
	case PIC_FROM_CAMERA:
	    Intent openCameraIntent = new Intent(
		    MediaStore.ACTION_IMAGE_CAPTURE);
	    startActivityForResult(openCameraIntent, PIC_FROM_CAMERA);
	    break;
	default:
	    break;
	}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	Bitmap pic = null;
	Intent intent;

	if (resultCode == RESULT_OK) {
	    switch (requestCode) {
	    // 相机 拍照送去剪裁
	    case PIC_FROM_CAMERA:// 相机
		cutPhoto(data, PIC_FROM_CAMERA + 10);

		break;
	    // 相册选取，送去剪裁
	    case PIC_FROM_ALUBM:// 相册

		break;

	    // 相机剪裁返回
	    case PIC_FROM_CAMERA + 10:
		ContentResolver cResolver = getContentResolver();
		Bitmap bmp = (Bitmap) data.getParcelableExtra("data");
		try {
		    Images.Media.insertImage(cResolver, bmp, "shgoo图片",
			    "生活谷活动详情");
		    sendBroadcast(new Intent(
			    Intent.ACTION_MEDIA_MOUNTED,
			    Uri.parse("file://"
				    + Environment.getExternalStorageDirectory())));
		} catch (Exception e) {
		    e.printStackTrace();
		}
		break;
	    // 相册剪裁返回
	    case PIC_FROM_ALUBM + 10:

		break;

	    default:
		break;
	    }
	}

    }

    private void cutPhoto(Intent data, int requestCode) {
	Intent intent = new Intent("com.android.camera.action.CROP");
	intent.setType("image/*");
	String url = data.getStringExtra("url");
	intent.putExtra("data", data.getParcelableExtra("data"));
	intent.putExtra("crop", "true");
	intent.putExtra("aspectX", 1);
	intent.putExtra("aspectY", 1);
	intent.putExtra("outputX",160);
	intent.putExtra("outputY", 160);
	intent.putExtra("return-data", true);
	startActivityForResult(intent, requestCode);
    }

    public void confirmFunction(View view) {
	try {
	    // TakePicMan.save("bitmap", canvas.getSubsetBitmap());
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	setResult(RESULT_OK);
	finish();
    }

    public void exitFunction(View view) {
	this.finish();
    }

    @Override
    public void onClick(View v) {
	// TODO Auto-generated method stub
	switch (v.getId()) {
	case id2:
	    confirmFunction(v);
	    break;
	case id1:
	    setResult(1);
	    finish();
	    break;

	default:
	    break;
	}
    }

}